package com.example.health.controller;

import com.auth0.jwt.JWT;
import com.example.health.entity.User;
import com.example.health.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class BaseController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private HttpServletRequest request;

    /**
     * 根据token获取用户信息
     * @ return user
     */
    public User getUser() {
        String token = request.getHeader("token");// 获取请求头中的token
        String aud = JWT.decode(token).getAudience().get(0);// 从token中获取用户id
        Integer userId = Integer.valueOf(aud);// 将用户id转换为整数
        return userMapper.selectById(userId);// 根据用户id查询用户信息
    }

}
